﻿/*
* 
* AudioPlayer to play and control an audio source.
* Version 1.0.0
* 
* A part of AT Tools.
* 
* Copyright (c) 2011 Peter Sandberg Brun
* Copyright (c) 2011 Mads Sandberg Brun
*
* Licensed under the MS-PL license:
* http://attools.codeplex.com/license
*
*/

(function ($) {
    $.fn.extend({
        audioPlayer: function (options) {
            //Set the default values
            var defaults = {
                audioSelector: '.audioElement',
                soundLinkSelector: '.soundLink',
                playSelector: '.play',
                pauseSelector: '.pause',
                stopSelector: '.stop',
                soundControlSelector: '.soundControl',
                audioUrl: '',
                playType: ''
            };
            var opts = $.extend({}, defaults, options);

            var audioSupport = !!(document.createElement('audio').canPlayType) && !!(document.createElement('audio').canPlayType(opts.playType));

            //Iterate over the current set of matched elements 
            var objs = this;
            return this.each(function () {

                var obj = $(this);

                var audioElement = $(opts.audioSelector, obj);
                var audioDom = audioElement.get(0);
                var soundLink = $(opts.soundLinkSelector, obj);
                var playElement = $(opts.playSelector, obj);
                var pauseElement = $(opts.pauseSelector, obj);
                var stopElement = $(opts.stopSelector, obj);
                var soundControlElement = $(opts.soundControlSelector, obj);

                pauseElement.hide();
                stopElement.hide();

                if (audioSupport) {
                    playElement.click(function () {
                        audioDom.play();
                        playElement.hide();
                        pauseElement.show();
                        return false;
                    });
                    pauseElement.click(function () {
                        audioDom.pause();
                        playElement.show();
                        pauseElement.hide();
                        return false;
                    });

                } else if ($.browser.msie) { // && $.browser.version >= 7
                    var bgsoundElement = $('<bgsound />');
                    bgsoundElement.appendTo(obj);

                    playElement.click(function () {
                        bgsoundElement.attr('src', opts.audioUrl);
                        playElement.hide();
                        stopElement.show();
                        return false;
                    });
                    stopElement.click(function () {
                        bgsoundElement.attr('src', '');
                        playElement.show();
                        stopElement.hide();
                        return false;
                    });
                }
            });
        }
    });
})(jQuery);